package com.minyifang.wcspf.work.ui.user.presenter;

import com.minyifang.wcspf.common.base.BasePresenter;
import com.minyifang.wcspf.common.config.network.Constants;
import com.minyifang.wcspf.common.config.network.RetrofitHelper;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.work.ui.user.view.MyCollectionView;
import com.minyifang.wcspf.work.ui.user.view.MyWalletView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class MyCollectionPresenter extends BasePresenter<MyCollectionView> {

    public void getCollectionList(int pageIndex) {
//        getView().showLoadingView();
        Map<String, Object> map = new HashMap<>();
        map.put("page", pageIndex);
        map.put("limit", 10);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), Constants.getAESJsonData(map, UserManager.getInstance().getToken()).toString());
        addTask(RetrofitHelper.getInstance().getService().getCollectionList(requestBody), respond -> {
//            getView().hideLoadingView();
            getView().showCollectionList(respond);
        });

    }

    public void delCollection(String maId) {
        Map<String, Object> map = new HashMap<>();
        map.put("dolectionId", maId);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().delCollection(requestBody), respond -> getView().showDesCollection(respond));
    }

    public void addToCar(String skuId, String quantity) {
        Map<String, Object> map = new HashMap<>();
        map.put("skuId", skuId);
        map.put("quantity", quantity);
        JSONObject jsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().addToCar(requestBody), stringHttpRespond -> getView().showAddToCarRes(stringHttpRespond));
    }

    public void getCarNum() {
        JSONObject jsonData = Constants.getJsonData(null, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getCarNum(requestBody), response -> getView().showCarNum(response));
    }
}
